//
//  TopMenuTitleViewController.m
//  WisdomZhuHai
//
//  Created by JasonLee on 15/1/29.
//  Copyright (c) 2015年 wyd. All rights reserved.
//

#import "TopMenuTitleViewController.h"
#import "TopMenuPageViewController.h"
#import "TopMenuMainViewController.h"
#import "TransforController.h"


@interface TopMenuTitleViewController ()

@property (nonatomic, strong) TopMenuPageViewController *instance_page;

@end

@implementation TopMenuTitleViewController

@synthesize titleArray;
@synthesize userSelectedButtonTag;
@synthesize scrollViewSelectedID;
@synthesize buttonOrignXArray;
@synthesize buttonWithArray;

//- (void)viewDidLoad {
//    [super viewDidLoad];
//    // Do any additional setup after loading the view.
//}
//
//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

+ (TopMenuTitleViewController *)getInstance{
//    static TopMenuTitleViewController *instance;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        instance = [[self alloc] initWithFrame:CGRectMake(0, 65, CONTENTSIZEX, 40)];
//    });
    TopMenuTitleViewController *instance = [[self alloc]initWithFrame:CGRectMake(0, 65, CONTENTSIZEX, 40)];
    return instance;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.delegate = self;
        self.backgroundColor = [TransforController getColorWithRed:225 andGreen:225 andBlue:225 andAlpha:1];
        self.pagingEnabled = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        
        userSelectedButtonTag = 100;
        scrollViewSelectedID = 100;
        
        self.buttonOrignXArray = [[NSMutableArray alloc] init];
        self.buttonWithArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)setPageView:(id)target{
    self.instance_page = (TopMenuPageViewController *)target;
}

- (void)initWithTitleButtons{
    
    float xPos = 5.0f;
    for(int i = 0; i < [self.titleArray count]; i++){
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        NSString *title = [self.titleArray objectAtIndex:i];
        [button setTag:i + 100];
        if(i == 0){
            button.selected = YES;
        }
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(selectNameButton:) forControlEvents:UIControlEventTouchUpInside];
        
//        int buttonWidth = [title sizeWithFont:button.titleLabel.font
//                            constrainedToSize:CGSizeMake(150, 36)
//                                lineBreakMode:NSLineBreakByClipping].width;
        int buttonWidth = ((320 - [titleArray count] * (BUTTONGAP + 5)) / [titleArray count]);

        button.titleLabel.textAlignment = UITextAlignmentCenter;
        button.frame = CGRectMake(xPos, 5, buttonWidth + BUTTONGAP, 36);
        button.titleLabel.font = [TransforController getFontWithFont:@"Arial" andSize:15.0];
        if([self.buttonOrignXArray count] < [self.titleArray count]){
            [self.buttonOrignXArray addObject:@(xPos)];
        }
        //按钮的X坐标
        xPos += buttonWidth + BUTTONGAP;
        //按钮的宽度
        if([self.buttonWithArray count] < [self.titleArray count]){
            [self.buttonWithArray addObject:@(button.frame.size.width)];
            [self addSubview:button];
        }
        
    }
    //视图的位移
    self.contentSize = CGSizeMake(320, 0);
    if(shadowImage != nil){
        [shadowImage removeFromSuperview];
    }
    shadowImage = [[UIImageView alloc] initWithFrame:CGRectMake(BUTTONGAP - 4, 36,
                                                                [[self.buttonWithArray objectAtIndex:0] floatValue] - 1,
                                                                4)];
    [shadowImage setImage:[UIImage imageNamed:@"蓝色.png"]];
    [self addSubview:shadowImage];
}

- (void)selectNameButton:(UIButton *)sender{
//    [self adjustScrollViewContentX:sender];
    
    //如果跟换按钮
    if(sender.tag != userSelectedButtonTag){
        UIButton *mybutton = (UIButton *)[self viewWithTag:userSelectedButtonTag];
        mybutton.selected = NO;
        userSelectedButtonTag = sender.tag;
        sender.titleLabel.textColor = [UIColor blackColor];
    }
    //按钮选中状态
    if(!sender.selected){
        sender.selected = YES;
        [UIView animateWithDuration:0.25 animations:^{
            [shadowImage setFrame:CGRectMake(sender.frame.origin.x,
                                             36,
                                             [[self.buttonWithArray objectAtIndex:sender.tag - 100] floatValue],
                                             4)];
            sender.titleLabel.textColor = [UIColor whiteColor];
        } completion:^(BOOL finished) {
            if(finished){
                //页面出现
                [self.instance_page setContentOffset:CGPointMake((sender.tag - 100) * 320, 0) animated:YES];
                //滑动选择页面
                scrollViewSelectedID = sender.tag;
            }
        }];
    }
}

//调整滚动按钮显示
- (void)adjustScrollViewContentX:(UIButton *)sender{
    float originX = [[self.buttonOrignXArray objectAtIndex:(sender.tag - 100)] floatValue];
    float width = [[self.buttonWithArray objectAtIndex:(sender.tag - 100)] floatValue];
    
    if((sender.frame.origin.x - self.contentOffset.x) > (CONTENTSIZEX - (BUTTONGAP + width))){
        [self setContentOffset:CGPointMake(originX - 30, 0) animated:YES];
    }
    
    if((sender.frame.origin.x - self.contentOffset.x) < 5){
        [self setContentOffset:CGPointMake(originX, 0) animated:YES];
    }
}

- (void)setButttonUnSelect{
    UIButton *button = (UIButton *)[self viewWithTag:scrollViewSelectedID];
    button.selected = NO;
}

- (void)setButtonSelect{
    //选中滑动的按钮
    UIButton *button = (UIButton *)[self viewWithTag:scrollViewSelectedID];
    [UIView animateWithDuration:0.25 animations:^{
        [shadowImage setFrame:CGRectMake(button.frame.origin.x, 36,
                                         [[self.buttonWithArray objectAtIndex:button.tag - 100] floatValue],
                                         4)];
//        button.titleLabel.textColor = [UIColor whiteColor];
    } completion:^(BOOL finished) {
        if(finished){
            if(!button){
                button.selected = YES;
                userSelectedButtonTag = button.tag;
            }
        }
    }];
}

- (void)setScrollViewContentOffset{
    float originX = [[self.buttonOrignXArray objectAtIndex:(scrollViewSelectedID - 100)] floatValue];
    float width = [[self.buttonWithArray objectAtIndex:(scrollViewSelectedID - 100)] floatValue];
    
    if((originX - self.contentOffset.x) > (CONTENTSIZEX - (BUTTONGAP + width))){
        [self setContentOffset:CGPointMake(originX - 30, 0) animated:YES];
    }
    
    if(originX - self.contentOffset.x < 5){
        [self setContentOffset:CGPointMake(originX, 0) animated:YES];
    }
}


@end
